import 'package:mall/constant/app_strings.dart';
import 'package:mall/utils/broadcast/broadcast_util.dart';

import '../../utils/sc/state_controller.dart';
import '../model/collection_entity.dart';
import '../service/goods_service.dart';

class CollectListSc extends PagingListSC<CollectionList> implements GoodsEvent{

  int? type = 0;

  @override
  void onInit() {
    super.onInit();
    BroadcastUtil.register<GoodsEvent>(this);
  }

  @override
  Future<List<CollectionList>> loadData() async{
    if (type == 0) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    final res = await GoodsService.queryCollect(type!,page, limit);
    count = res.total;
    return res.xList ?? [];
  }
  
  @override
  void addOrDeleteCollect(int goodsId) {
    final index = data.indexWhere((element) => element.id == goodsId);
    if (index != -1) {
      data.removeAt(index);
    } else {
      refreshData();
    }
  }
}